import fiftyone as fo
import fiftyone.utils.random as four
import fiftyone.zoo as foz
dataset = fo.Dataset("first-group-dataset")
dataset.add_group_field("group", default="center")
groups = ["left", "center", "right"]
d = foz.load_zoo_dataset("quickstart")
four.random_split(d, {g: 1 / len(groups) for g in groups})
filepaths = [d.match_tags(g).values("filepath") for g in groups]
filepaths = [dict(zip(groups, fps)) for fps in zip(*filepaths)]
samples = []
for fps in filepaths:
group = fo.Group()
for name, filepath in fps.items():
sample = fo.Sample(filepath=filepath, group=group.element(name))
samples.append(sample)
dataset.add_samples(samples)
Dataset already downloaded Loading 'quickstart' 100% |█████████████████| 200/200 [2.5s elapsed, 0s remaining, 77.1 samples/s] Dataset 'quickstart' created 100% |█████████████████| 198/198 [52.7ms elapsed, 0s remaining, 3.8K samples/s]
['64f0ff20cb1bdc1e2ee070e2', '64f0ff20cb1bdc1e2ee070e3', '64f0ff20cb1bdc1e2ee070e4', '64f0ff20cb1bdc1e2ee070e5', '64f0ff20cb1bdc1e2ee070e6', '64f0ff20cb1bdc1e2ee070e7', '64f0ff20cb1bdc1e2ee070e8', '64f0ff20cb1bdc1e2ee070e9', '64f0ff20cb1bdc1e2ee070ea', '64f0ff20cb1bdc1e2ee070eb', '64f0ff20cb1bdc1e2ee070ec', '64f0ff20cb1bdc1e2ee070ed', '64f0ff20cb1bdc1e2ee070ee', '64f0ff20cb1bdc1e2ee070ef', '64f0ff20cb1bdc1e2ee070f0', '64f0ff20cb1bdc1e2ee070f1', '64f0ff20cb1bdc1e2ee070f2', '64f0ff20cb1bdc1e2ee070f3', '64f0ff20cb1bdc1e2ee070f4', '64f0ff20cb1bdc1e2ee070f5', '64f0ff20cb1bdc1e2ee070f6', '64f0ff20cb1bdc1e2ee070f7', '64f0ff20cb1bdc1e2ee070f8', '64f0ff20cb1bdc1e2ee070f9', '64f0ff20cb1bdc1e2ee070fa', '64f0ff20cb1bdc1e2ee070fb', '64f0ff20cb1bdc1e2ee070fc', '64f0ff20cb1bdc1e2ee070fd', '64f0ff20cb1bdc1e2ee070fe', '64f0ff20cb1bdc1e2ee070ff', '64f0ff20cb1bdc1e2ee07100', '64f0ff20cb1bdc1e2ee07101', '64f0ff20cb1bdc1e2ee07102', '64f0ff20cb1bdc1e2ee07103', '64f0ff20cb1bdc1e2ee07104', '64f0ff20cb1bdc1e2ee07105', '64f0ff20cb1bdc1e2ee07106', '64f0ff20cb1bdc1e2ee07107', '64f0ff20cb1bdc1e2ee07108', '64f0ff20cb1bdc1e2ee07109', '64f0ff20cb1bdc1e2ee0710a', '64f0ff20cb1bdc1e2ee0710b', '64f0ff20cb1bdc1e2ee0710c', '64f0ff20cb1bdc1e2ee0710d', '64f0ff20cb1bdc1e2ee0710e', '64f0ff20cb1bdc1e2ee0710f', '64f0ff20cb1bdc1e2ee07110', '64f0ff20cb1bdc1e2ee07111', '64f0ff20cb1bdc1e2ee07112', '64f0ff20cb1bdc1e2ee07113', '64f0ff20cb1bdc1e2ee07114', '64f0ff20cb1bdc1e2ee07115', '64f0ff20cb1bdc1e2ee07116', '64f0ff20cb1bdc1e2ee07117', '64f0ff20cb1bdc1e2ee07118', '64f0ff20cb1bdc1e2ee07119', '64f0ff20cb1bdc1e2ee0711a', '64f0ff20cb1bdc1e2ee0711b', '64f0ff20cb1bdc1e2ee0711c', '64f0ff20cb1bdc1e2ee0711d', '64f0ff20cb1bdc1e2ee0711e', '64f0ff20cb1bdc1e2ee0711f', '64f0ff20cb1bdc1e2ee07120', '64f0ff20cb1bdc1e2ee07121', '64f0ff20cb1bdc1e2ee07122', '64f0ff20cb1bdc1e2ee07123', '64f0ff20cb1bdc1e2ee07124', '64f0ff20cb1bdc1e2ee07125', '64f0ff20cb1bdc1e2ee07126', '64f0ff20cb1bdc1e2ee07127', '64f0ff20cb1bdc1e2ee07128', '64f0ff20cb1bdc1e2ee07129', '64f0ff20cb1bdc1e2ee0712a', '64f0ff20cb1bdc1e2ee0712b', '64f0ff20cb1bdc1e2ee0712c', '64f0ff20cb1bdc1e2ee0712d', '64f0ff20cb1bdc1e2ee0712e', '64f0ff20cb1bdc1e2ee0712f', '64f0ff20cb1bdc1e2ee07130', '64f0ff20cb1bdc1e2ee07131', '64f0ff20cb1bdc1e2ee07132', '64f0ff20cb1bdc1e2ee07133', '64f0ff20cb1bdc1e2ee07134', '64f0ff20cb1bdc1e2ee07135', '64f0ff20cb1bdc1e2ee07136', '64f0ff20cb1bdc1e2ee07137', '64f0ff20cb1bdc1e2ee07138', '64f0ff20cb1bdc1e2ee07139', '64f0ff20cb1bdc1e2ee0713a', '64f0ff20cb1bdc1e2ee0713b', '64f0ff20cb1bdc1e2ee0713c', '64f0ff20cb1bdc1e2ee0713d', '64f0ff20cb1bdc1e2ee0713e', '64f0ff20cb1bdc1e2ee0713f', '64f0ff20cb1bdc1e2ee07140', '64f0ff20cb1bdc1e2ee07141', '64f0ff20cb1bdc1e2ee07142', '64f0ff20cb1bdc1e2ee07143', '64f0ff20cb1bdc1e2ee07144', '64f0ff20cb1bdc1e2ee07145', '64f0ff20cb1bdc1e2ee07146', '64f0ff20cb1bdc1e2ee07147', '64f0ff20cb1bdc1e2ee07148', '64f0ff20cb1bdc1e2ee07149', '64f0ff20cb1bdc1e2ee0714a', '64f0ff20cb1bdc1e2ee0714b', '64f0ff20cb1bdc1e2ee0714c', '64f0ff20cb1bdc1e2ee0714d', '64f0ff20cb1bdc1e2ee0714e', '64f0ff20cb1bdc1e2ee0714f', '64f0ff20cb1bdc1e2ee07150', '64f0ff20cb1bdc1e2ee07151', '64f0ff20cb1bdc1e2ee07152', '64f0ff20cb1bdc1e2ee07153', '64f0ff20cb1bdc1e2ee07154', '64f0ff20cb1bdc1e2ee07155', '64f0ff20cb1bdc1e2ee07156', '64f0ff20cb1bdc1e2ee07157', '64f0ff20cb1bdc1e2ee07158', '64f0ff20cb1bdc1e2ee07159', '64f0ff20cb1bdc1e2ee0715a', '64f0ff20cb1bdc1e2ee0715b', '64f0ff20cb1bdc1e2ee0715c', '64f0ff20cb1bdc1e2ee0715d', '64f0ff20cb1bdc1e2ee0715e', '64f0ff20cb1bdc1e2ee0715f', '64f0ff20cb1bdc1e2ee07160', '64f0ff20cb1bdc1e2ee07161', '64f0ff20cb1bdc1e2ee07162', '64f0ff20cb1bdc1e2ee07163', '64f0ff20cb1bdc1e2ee07164', '64f0ff20cb1bdc1e2ee07165', '64f0ff20cb1bdc1e2ee07166', '64f0ff20cb1bdc1e2ee07167', '64f0ff20cb1bdc1e2ee07168', '64f0ff20cb1bdc1e2ee07169', '64f0ff20cb1bdc1e2ee0716a', '64f0ff20cb1bdc1e2ee0716b', '64f0ff20cb1bdc1e2ee0716c', '64f0ff20cb1bdc1e2ee0716d', '64f0ff20cb1bdc1e2ee0716e', '64f0ff20cb1bdc1e2ee0716f', '64f0ff20cb1bdc1e2ee07170', '64f0ff20cb1bdc1e2ee07171', '64f0ff20cb1bdc1e2ee07172', '64f0ff20cb1bdc1e2ee07173', '64f0ff20cb1bdc1e2ee07174', '64f0ff20cb1bdc1e2ee07175', '64f0ff20cb1bdc1e2ee07176', '64f0ff20cb1bdc1e2ee07177', '64f0ff20cb1bdc1e2ee07178', '64f0ff20cb1bdc1e2ee07179', '64f0ff20cb1bdc1e2ee0717a', '64f0ff20cb1bdc1e2ee0717b', '64f0ff20cb1bdc1e2ee0717c', '64f0ff20cb1bdc1e2ee0717d', '64f0ff20cb1bdc1e2ee0717e', '64f0ff20cb1bdc1e2ee0717f', '64f0ff20cb1bdc1e2ee07180', '64f0ff20cb1bdc1e2ee07181', '64f0ff20cb1bdc1e2ee07182', '64f0ff20cb1bdc1e2ee07183', '64f0ff20cb1bdc1e2ee07184', '64f0ff20cb1bdc1e2ee07185', '64f0ff20cb1bdc1e2ee07186', '64f0ff20cb1bdc1e2ee07187', '64f0ff20cb1bdc1e2ee07188', '64f0ff20cb1bdc1e2ee07189', '64f0ff20cb1bdc1e2ee0718a', '64f0ff20cb1bdc1e2ee0718b', '64f0ff20cb1bdc1e2ee0718c', '64f0ff20cb1bdc1e2ee0718d', '64f0ff20cb1bdc1e2ee0718e', '64f0ff20cb1bdc1e2ee0718f', '64f0ff20cb1bdc1e2ee07190', '64f0ff20cb1bdc1e2ee07191', '64f0ff20cb1bdc1e2ee07192', '64f0ff20cb1bdc1e2ee07193', '64f0ff20cb1bdc1e2ee07194', '64f0ff20cb1bdc1e2ee07195', '64f0ff20cb1bdc1e2ee07196', '64f0ff20cb1bdc1e2ee07197', '64f0ff20cb1bdc1e2ee07198', '64f0ff20cb1bdc1e2ee07199', '64f0ff20cb1bdc1e2ee0719a', '64f0ff20cb1bdc1e2ee0719b', '64f0ff20cb1bdc1e2ee0719c', '64f0ff20cb1bdc1e2ee0719d', '64f0ff20cb1bdc1e2ee0719e', '64f0ff20cb1bdc1e2ee0719f', '64f0ff20cb1bdc1e2ee071a0', '64f0ff20cb1bdc1e2ee071a1', '64f0ff20cb1bdc1e2ee071a2', '64f0ff20cb1bdc1e2ee071a3', '64f0ff20cb1bdc1e2ee071a4', '64f0ff20cb1bdc1e2ee071a5', '64f0ff20cb1bdc1e2ee071a6', '64f0ff20cb1bdc1e2ee071a7']
print(dataset)
Name: first-group-dataset Media type: group Group slice: center Num groups: 66 Persistent: False Tags: [] Sample fields: id: fiftyone.core.fields.ObjectIdField filepath: fiftyone.core.fields.StringField tags: fiftyone.core.fields.ListField(fiftyone.core.fields.StringField) metadata: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.metadata.Metadata) group: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.groups.Group)
session = fo.launch_app(dataset)
print(dataset.group_slices)
['left', 'center', 'right']
print(dataset.group_media_types)
{'left': 'image', 'center': 'image', 'right': 'image'}
sample = dataset.shuffle().first()
print(sample)
<SampleView: { 'id': '64f0ff20cb1bdc1e2ee070e6', 'media_type': 'image', 'filepath': '/home/dan/fiftyone/quickstart/data/003344.jpg', 'tags': [], 'metadata': None, 'group': <Group: {'id': '64f0ff20cb1bdc1e2ee070a1', 'name': 'center'}>, }>
dataset.group_slice = "left"
sample = dataset.shuffle().first()
print(sample)
<SampleView: { 'id': '64f0ff20cb1bdc1e2ee07166', 'media_type': 'image', 'filepath': '/home/dan/fiftyone/quickstart/data/003420.jpg', 'tags': [], 'metadata': None, 'group': <Group: {'id': '64f0ff20cb1bdc1e2ee070cc', 'name': 'left'}>, }>
session = fo.launch_app(dataset)
dataset.group_slice = "center"
sample = dataset.shuffle().first()
group_id = sample.group.id
group = dataset.get_group(group_id)
print(group)
{'left': <Sample: { 'id': '64f0ff20cb1bdc1e2ee07109', 'media_type': 'image', 'filepath': '/home/dan/fiftyone/quickstart/data/004781.jpg', 'tags': [], 'metadata': None, 'group': <Group: {'id': '64f0ff20cb1bdc1e2ee070ad', 'name': 'left'}>, }>, 'center': <Sample: { 'id': '64f0ff20cb1bdc1e2ee0710a', 'media_type': 'image', 'filepath': '/home/dan/fiftyone/quickstart/data/003614.jpg', 'tags': [], 'metadata': None, 'group': <Group: {'id': '64f0ff20cb1bdc1e2ee070ad', 'name': 'center'}>, }>, 'right': <Sample: { 'id': '64f0ff20cb1bdc1e2ee0710b', 'media_type': 'image', 'filepath': '/home/dan/fiftyone/quickstart/data/002514.jpg', 'tags': [], 'metadata': None, 'group': <Group: {'id': '64f0ff20cb1bdc1e2ee070ad', 'name': 'right'}>, }>}
import fiftyone as fo
import fiftyone.zoo as foz
from fiftyone import ViewField as F
dataset = foz.load_zoo_dataset("quickstart-groups")
print(dataset.group_slice)
# left
session = fo.launch_app(dataset)
Dataset already downloaded Loading 'quickstart-groups' 100% |█████████████████| 600/600 [2.0s elapsed, 0s remaining, 308.7 samples/s] Dataset 'quickstart-groups' created left
# Filters based on the content in the 'left' slice
view = (
dataset
.match_tags("train")
.filter_labels("ground_truth", F("label") == "Pedestrian")
)
from fiftyone import ViewField as F
dataset.compute_metadata()
# Match groups whose `left` image has a height of at least 640 pixels and
# whose `right` image has a height of at most 480 pixels
view = dataset.match(
(F("groups.left.metadata.height") >= 640)
& (F("groups.right.metadata.height") <= 480)
)
print(view)
Dataset: quickstart-groups Media type: group Group slice: left Num groups: 0 Group fields: id: fiftyone.core.fields.ObjectIdField filepath: fiftyone.core.fields.StringField tags: fiftyone.core.fields.ListField(fiftyone.core.fields.StringField) metadata: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.metadata.Metadata) group: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.groups.Group) ground_truth: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.labels.Detections) View stages: 1. Match(filter={'$expr': {'$and': [...]}})
bbox_width = F("bounding_box")[2] * F("$metadata.width")
bbox_height = F("bounding_box")[3] * F("$metadata.height")
bbox_area = bbox_width * bbox_height
print(dataset.group_slice)
# left
print(dataset.count("ground_truth.detections"))
print(dataset.mean("ground_truth.detections[]", expr=bbox_area))
left 1379 9291.526529006525
dataset.group_slice = "right"
print(dataset.count("ground_truth.detections"))
print(dataset.bounds("ground_truth.detections[]", expr=bbox_area))
1100 (221.24430441846425, 466616.0)
print(dataset.count()) # 200
print(dataset.count("ground_truth.detections")) # 1438
view3 = dataset.select_group_slices(["left", "right"])
print(view3.count()) # 400
print(view3.count("ground_truth.detections")) # 2876
200 1100 400 2479
bbox_width = F("bounding_box")[2]
bbox_height = F("bounding_box")[3]
bbox_area = bbox_width * bbox_height
view = dataset.filter_labels("ground_truth", (0.05 <= bbox_area) & (bbox_area < 0.5))
print(view)
Dataset: quickstart-groups Media type: group Group slice: right Num groups: 86 Group fields: id: fiftyone.core.fields.ObjectIdField filepath: fiftyone.core.fields.StringField tags: fiftyone.core.fields.ListField(fiftyone.core.fields.StringField) metadata: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.metadata.Metadata) group: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.groups.Group) ground_truth: fiftyone.core.fields.EmbeddedDocumentField(fiftyone.core.labels.Detections) View stages: 1. FilterLabels(field='ground_truth', filter={'$and': [{...}, {...}]}, only_matches=True, trajectories=False)